package com.wuzf.libnetwork;


import java.util.Iterator;
import java.util.Map;

public class GetRequest<T> extends Request<T> {

    private String header_url;

    public GetRequest() {
    }

    public GetRequest(String headerUrl) {
        this.header_url = headerUrl;
    }

    @Override
    public okhttp3.Request generateRequest(okhttp3.Request.Builder builder) {
        // get的情况需要拼凑?key=value&key1=value1的形式
        Map<String, Object> paramsEntity = getParamsEntity();
        if (paramsEntity.size() == 0) {
            return builder.build();
        }

        String url = header_url;
        StringBuilder sb = new StringBuilder(url);
        if (sb.indexOf("?") < 0) {
            sb.append("?");
        }

        Iterator<Map.Entry<String, Object>> iterator = paramsEntity.entrySet().iterator();
        int index = 0;
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            if (index != 0) sb.append("&");
            sb.append(entry.getKey()).append("=").append(entry.getValue());
            index++;
        }
        builder.url(sb.toString());
        return builder.get().build();
    }
}
